package com.qut.atcoder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

@SpringBootApplication
public class AtcoderApplication {

    public static void main(String[] args) {
        SpringApplication.run(AtcoderApplication.class, args);
//        String block ="#include <stdio.h>\n" +
//                "int main()\n" +
//                "{int a;scanf(\"%d\",&a);\n" +
//                "printf(\"%d\",a);return 0;}\n";
//        String input = "C:\\Users\\Public\\Documents\\atcode\\judger\\main.cpp";
//        String output = "C:\\Users\\Public\\Documents\\atcode\\judger\\main.exe";
//        String[] redirect = new String[]{output};
//        String[] strings = new String[]{"gcc", input, "-o", output};
//        try {
//            Files.write(Paths.get(input),block.getBytes());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            Process process = Runtime.getRuntime().exec(strings);
//            process.waitFor();
//        } catch (IOException | InterruptedException e) {
//            e.printStackTrace();
//        }
//        try {
//            Process process = Runtime.getRuntime().exec(redirect);
//            //输出到进程的数据
//            OutputStream outputStream = process.getOutputStream();
//            PrintStream printStream = new PrintStream(outputStream, true);
//            InputStream in = new FileInputStream("C:\\Users\\Public\\Documents\\atcode\\judger\\in.txt");
//            byte[] ans = in.readAllBytes();
//            printStream.println(123);
//
//            InputStream inputStream = process.getInputStream();
//            byte[] bytes = inputStream.readAllBytes();
//            System.out.println(Arrays.toString(ans));
//            System.out.println(Arrays.toString(bytes));
//            process.waitFor();
//        } catch (IOException | InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        System.out.println("=============== 设置跨域过滤器 ================");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}
